﻿//using Sustainalytics.ClientDW.DataAccessLayer;
//using Sustainalytics.ClientDW.Entities;
//using Sustainalytics.DataLayer;
//using Sustainalytics.DataLayer.ConnectionStrings;
//using Sustainalytics.Entities;
//using Sustainalytics.Entities.ConnectionStrings;
//using Sustainalytics.Portfolios.BL.Exceptions;
//using Sustainalytics.Utils;
//using System;
//using System.Collections.Generic;
//using System.Configuration;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;

//namespace Sustainalytics.Portfolios.BL
//{
//    public class PortfolioCompanyManagement: IPortfolioCompanyManagement
//    {
//        private IPortfolioCompanyRepository _portfolioCompanyRepo { get; set; }

//        private IPortfolioRepository _portfolioRepo { get; set; }

//        private IClientDwRepository _clientDWRepo { get; set; }

        //public PortfolioCompanyManagement(IPortfolioCompanyRepository portfolioCompanyManagement)
        //{
        //    this._portfolioCompanyRepo = portfolioCompanyManagement;
        //    if (Utils.UnitTestDetector.IsInUnitTest)
        //    {
        //        this._portfolioRepo = PortfolioRepository.GetPortfolioRepository("mongodb://localhost/ClientPlatform_Test");
        //        this._clientDWRepo = ClientDwRepository.GetInstance("mongodb://localhost/ClientDW_Test");
        //    }
        //    else
        //    {
        //        var clientPlatformCnx = ConfigurationManager.ConnectionStrings["ClientPlatform"].ConnectionString;
        //        this._portfolioRepo = PortfolioRepository.GetPortfolioRepository(connectionString: clientPlatformCnx);

        //        var connectionStringsManager =
        //            ConnectionStringsManager.GetInstance(ConnectionStringsRepository.GetInstance(clientPlatformCnx),
        //                                                    DateTime.Today.AddHours(double.Parse(ConfigurationManager.AppSettings["ConnectionStringsPollingStartHourUtc"])),
        //                                                    TimeSpan.FromHours(double.Parse(ConfigurationManager.AppSettings["ConnectionStringsPollingDurationHours"])),
        //                                                    TimeSpan.FromMinutes(double.Parse(ConfigurationManager.AppSettings["ConnectionStringsPollingFrequencyMinutes"])));

        //        this._clientDWRepo = ClientDwRepository.GetInstance(connectionStringsManager.GetConnectionString(ConnectionStringType.ClientDw));
        //    }
        //}

        //public void AddPortfolioCompanies(List<PortfolioCompany> portfolioCompanies)
        //{
        //    _portfolioCompanyRepo.AddPortfolioCompanies(portfolioCompanies);
        //}

        //public void InsertPortfolioCompany(Guid portfolioId, Guid id, PortfolioUserClaims userClaims)
        //{
        //    bool companyExists = _portfolioCompanyRepo.CompanyExistsInPortfolio(portfolioId, id);
        //    if (companyExists)
        //    {
        //        throw new PortfolioDetailsExistsInPortfolio();
        //    }
        //    else
        //    {
        //        Portfolio portfolio = _portfolioRepo.GetPortfolio(portfolioId);
        //        if (portfolio.AccountId == userClaims.AccountId && portfolio.AddedByUserName.ToLowerInvariant() == userClaims.AddedByUserName.ToLowerInvariant()
        //            )
        //        {
        //            if(Sustainalytics.Utils.UnitTestDetector.IsInUnitTest)
        //            {
        //                PortfolioCompany portfolioCompany = new PortfolioCompany();
        //                portfolioCompany.Id = new Guid();
        //                portfolioCompany.CompanyId = id;
        //                portfolioCompany.CompanyName = "Test post";
        //                portfolioCompany.CompanyNameLowercase = "test post";
        //                portfolioCompany.CompanyNameUrlFriendly = "Test post";
        //                portfolioCompany.PortfolioId = portfolioId;
        //                portfolioCompany.UniverseIds = null;
        //                ////portfolioCompany.ISINs = new List<string> { "AB4985", "AB52212" };
        //                //portfolioCompany.FirstISINLowercase = "AB4985";

        //                _portfolioCompanyRepo.AddPortfolioCompany(portfolioCompany);
        //            }
        //            else
        //            {
        //                ReferenceData company = _clientDWRepo.GetReferenceDataByCompanyId(id);
        //                CorporateData companyCD = _clientDWRepo.GetCorporateDataByCompanyId(id);
        //                PortfolioCompany portfolioCompany = new PortfolioCompany();
        //                portfolioCompany.Id = new Guid();
        //                portfolioCompany.CompanyId = id;
        //                portfolioCompany.CompanyName = companyCD.CompanyName;
        //                portfolioCompany.CompanyNameLowercase = companyCD.CompanyNameLowercase;
        //                portfolioCompany.CompanyNameUrlFriendly = companyCD.CompanyNameUrlFriendly;
        //                portfolioCompany.PortfolioId = portfolioId;
        //                portfolioCompany.UniverseIds = companyCD.UniverseIds;
        //                //portfolioCompany.ISINs = companyCD.ISINs;
        //                //portfolioCompany.FirstISINLowercase = company.ISINs.FirstOrDefault() != null ? company.ISINs.FirstOrDefault().ToLower() : string.Empty;
        //                portfolioCompany.CapitalIQ = companyCD.CapitalIQID;

        //                string argsStr = LogArgument.GetString(
        //                    new
        //                    {
        //                        PortfolioId = portfolioCompany.PortfolioId,
        //                        CompanyId = portfolioCompany.CompanyId
        //                    });

        //                PortfolioDetailsManagementEventSource.Log.AddPortfolioCompany(argsStr);
        //                _portfolioCompanyRepo.AddPortfolioCompany(portfolioCompany);
        //                PortfolioDetailsManagementEventSource.Log.AddPortfolioCompanySuccess(argsStr);

        //                portfolio.CompanyCount++;
        //                portfolio.DateModified = DateTime.Now;
        //                _portfolioRepo.UpdatePortfolio(portfolio);
        //            }
        //        }
        //        else
        //        {
        //            throw new PortfolioDetailsInsertUnauthorisedException();
        //        }
        //    }
            
        //}

        //public List<PortfolioCompany> GetCompaniesForPortfolio(Guid portfolioId)
        //{
        //    return _portfolioCompanyRepo.GetCompaniesForPortfolio(portfolioId);
        //}

        //public int GetPortfolioCompaniesCount(PortfolioCompanyFilter filter, PortfolioUserClaims userClaims)
        //{
        //    string argsStr = LogArgument.GetString(filter.PortfolioId);
        //    try
        //    {
        //        Portfolio portfolio = _portfolioRepo.GetPortfolio(filter.PortfolioId);
        //        if (portfolio != null && (portfolio.AddedByUserId == userClaims.AddedByUserId || (portfolio.SharingStatus == PortfolioSharingStatus.Shared && portfolio.AccountId == userClaims.AccountId)))
        //        {
        //            PortfolioDetailsManagementEventSource.Log.GetPortfolioCompaniesCount(argsStr);
        //            int count = this._portfolioCompanyRepo.GetPortfolioCompaniesCount(filter);
        //            PortfolioDetailsManagementEventSource.Log.GetPortfolioCompaniesCountSuccess(argsStr);
        //            return count;
        //        }
        //        return 0;
        //    }
        //    catch (Exception ex)
        //    {
        //        PortfolioDetailsManagementEventSource.Log.GetPortfolioCompaniesCountError(argsStr, ex);
        //        throw;
        //    }
        //}

        //public List<PortfolioCompany> GetPortfolioCompanies(PortfolioCompanyFilter filter, PortfolioUserClaims userClaims, int? skip = null, int? take = 100, string[] orderBy = null, bool orderByAsc = true)
        //{
        //    string argsStr = LogArgument.GetString(filter.PortfolioId);
        //    try
        //    {
        //        Portfolio portfolio = _portfolioRepo.GetPortfolio(filter.PortfolioId);
        //        if (portfolio != null && (portfolio.AddedByUserId == userClaims.AddedByUserId || (portfolio.SharingStatus == PortfolioSharingStatus.Shared && portfolio.AccountId == userClaims.AccountId)))
        //        {
        //            PortfolioDetailsManagementEventSource.Log.GetPortfolioCompanies(argsStr);
        //            List<PortfolioCompany> listPortfolioCompany = this._portfolioCompanyRepo.GetPortfolioCompanies(filter, skip, take, orderBy, orderByAsc);
        //            PortfolioDetailsManagementEventSource.Log.GetPortfolioCompaniesSuccess(argsStr);
        //            return listPortfolioCompany;
        //        }
        //        return null;
        //    }
        //    catch (Exception ex)
        //    {
        //        PortfolioDetailsManagementEventSource.Log.GetPortfolioCompaniesError(argsStr, ex);
        //        throw;
        //    }
        //}

        //public void DeletePortfolioCompanyByPortfolioId(Guid portfolioId)
        //{
        //    string argsStr = LogArgument.GetString(portfolioId);
        //    try
        //    {
        //        PortfolioDetailsManagementEventSource.Log.DeletePortfolioCompany(argsStr);
        //        _portfolioCompanyRepo.DeletePortfolioCompanyByPortfolioId(portfolioId);
        //        PortfolioDetailsManagementEventSource.Log.DeletePortfolioCompanySuccess(argsStr);
        //    }
        //    catch (Exception ex)
        //    {
        //        PortfolioDetailsManagementEventSource.Log.DeletePortfolioCompanyError(argsStr, ex);
        //        throw;
        //    }
        //}

        //public void DeletePortfolioCompany(Guid portfolioId, Guid id, PortfolioUserClaims userClaims)
        //{
        //    Portfolio portfolio = _portfolioRepo.GetPortfolio(portfolioId);
        //    if (portfolio.AddedByUserName.ToLowerInvariant() == userClaims.AddedByUserName.ToLowerInvariant() && portfolio.Id == portfolioId)
        //    {
        //        try
        //        {
        //            PortfolioDetailsManagementEventSource.Log.DeletePortfolioCompany(id.ToString());
        //            _portfolioCompanyRepo.DeletePortfolioCompany(id);
        //            PortfolioDetailsManagementEventSource.Log.DeletePortfolioCompanySuccess(id.ToString());
        //            portfolio.CompanyCount--;
        //            portfolio.DateModified = DateTime.Now;
        //            _portfolioRepo.UpdatePortfolio(portfolio);
        //        }
        //        catch (Exception ex)
        //        {
        //            PortfolioDetailsManagementEventSource.Log.DeletePortfolioCompanyError(id.ToString(), ex);
        //            throw;
        //        }
        //    }
        //    else
        //    {
        //        throw new PortfolioDetailsDeleteUnauthorisedException();
        //    }
        //}
//    }
//}
